package com.shangxia.leetcode.greedyAlgorithm;

import lombok.extern.slf4j.Slf4j;

/**
 * @ProjectName: leetcode
 * @ClassName: MaxProfit122
 * @Time: 2020/12/8
 * @Author: zhouy
 * @Description: 122. 买卖股票的最佳时机 II
 * @Experience:
 */

@Slf4j
public class MaxProfit122 {
    public static void main(String[] args) {
        System.out.println(maxProfit(new int[]{1,2,3,4,5}));
    }

    public static int maxProfit(int[] prices) {
        if (prices.length < 2) {
            return 0;
        }
        int start = prices[0];
        int end = prices[1];
        if (prices.length == 2) {
            return prices[0] < prices[1] ? prices[1] - prices[0] : 0;
        }
        int sum = prices[0] < prices[1] ? prices[1] - prices[0] : 0;
        for (int i = 2; i < prices.length; i++) {
            start = end;
            end = prices[i];
            if (start < end) {
                sum = sum + (end - start);
            }
        }
        return sum;
    }
    public static int maxProfit2(int[] prices) {
        int ans = 0;
        int n = prices.length;
        for (int i = 1; i < n; ++i) {
            ans += Math.max(0, prices[i] - prices[i - 1]);
        }
        return ans;
    }
}
